﻿using MB.Orm.DB;
using MB.RuleBase.Common;
using Model;
using Public.DataAccess;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Service
{

    public class BEstimateProduction
    {
        /// <summary>
        /// 查询预估订单信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="parValues"></param>
        /// <returns></returns>
        public List<EstimateProductionInfo> GetEstimateProductionInfoList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<EstimateProductionInfo> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<EstimateProductionInfo>
                                                ("EstimateProductionInfo", "GetEstimateProductionInfo", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }

        /// <summary>
        /// 在线库存
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="parValues"></param>
        /// <returns></returns>
        public List<ReceiveMaterial> GetZXKCReceiveMaterial(int pageIndex, int pageSize, out int total, string selType)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterial> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml2<ReceiveMaterial>
                                                ("EstimateProductionInfo", "GetZXKCReceiveMaterial", pageIndex, pageSize, out total);
                return list;
            }
        }



        /// <summary>
        /// 在线库存
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMaterial> GetZXKCReceiveMaterialByEx(string selType)
        {

            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterial> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMaterial>("EstimateProductionInfo", "GetZXKCReceiveMaterial");
                return lmSeedlist;
            }


        }




        /// <summary>
        /// 在线库存
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMaterial> GetGoodsSumWocByEx()
        {

            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterial> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMaterial>("EstimateProductionInfo", "GetGoodsSumWoc");
                return lmSeedlist;
            }


        }



        /// <summary>
        /// 查询全部批次订单数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="parValues"></param>
        /// <returns></returns>
        public List<BatchOrderInfo> GetDCOrderBatchDetailByAll(int pageIndex, int pageSize, out int total, string staDate, string endDate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<BatchOrderInfo> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml2<BatchOrderInfo>
                                                ("EstimateProductionInfo", "GetDCOrderBatchDetailByAll", pageIndex, pageSize, out total, staDate, endDate);
                return list;
            }
        }


        /// <summary>
        /// 根据时间批次查询单一批次数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="parValues"></param>
        /// <returns></returns>
        public List<BatchOrderInfo> GetDCOrderBatchDetail(int pageIndex, int pageSize, out int total, string staDate, string endDate,string batchNo)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<BatchOrderInfo> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml2<BatchOrderInfo>
                                                ("EstimateProductionInfo", "GetDCOrderBatchDetail", pageIndex, pageSize, out total, staDate, endDate, batchNo);
                return list;
            }
        }


        /// <summary>
        /// 修改预购生产单实际订单数量
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public int UpdateOrderProductionInfoByDrd(string sysDate, string staDate, string endDate, string importbatch)
        {
            int n = 0;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;

            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
                {
                    n = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("EstimateProductionInfo", "UpdateOrderProductionInfoByDrd", sysDate, staDate, endDate, importbatch);

                    if (n > 0)
                    {
                        tran.Commit();
                    }
                    else
                    {
                        tran.Rollback();
                    }
                }

            }
            catch (Exception)
            {
                tran.Rollback();
                throw;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return n;
        }




        /// <summary>
        /// 修改预购生产单实际订单数量
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public int UpdateOrderProductionInfoByCrd(string sysDate, string importbatch)
        {
            int n = 0;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;

            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
                {
                    n = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("EstimateProductionInfo", "UpdateOrderProductionInfoByCrd", sysDate, importbatch);

                    if (n > 0)
                    {
                        tran.Commit();
                    }
                    else
                    {
                        tran.Rollback();
                    }
                }

            }
            catch (Exception)
            {
                tran.Rollback();
                throw;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return n;
        }


        /// <summary>
        /// 修改预估单的状态以及修改数量实际数量等
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public int UpdateEstimateProductionInfo(EstimateProductionInfo einfo)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<EstimateProductionInfo>("EstimateProductionInfo", "UpdateEstimateProductionInfo", einfo);
        }



        /// <summary>
        /// 按照批次号审核所有预估生产单
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public int UpdateEstimateStatus(string importBatch)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("EstimateProductionInfo", "UpdateEstimateStatus", importBatch);
        }


        /// <summary>
        /// 新增预估单
        /// </summary>
        /// <param name="estimateProductionInfo"></param>
        /// <returns></returns>
        public bool InsertEstimateProduction(List<EstimateProductionInfo> estimateProductionInfo)
        {
            bool result = false;
            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                if (estimateProductionInfo == null || estimateProductionInfo.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("EstimateProductionInfo", "InsertEstimateProduction", estimateProductionInfo);
                    result = true;
                }
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }

        /// <summary>
        /// 按照批次号审核所有预估生产单
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public int InsertLmEstNumber(string numberNo)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("EstimateProductionInfo", "InsertLmEstNumber", numberNo);
        }


        /// <summary>
        /// 查询批量预估订单日期
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<EstimateProductionInfo> GetEstimateProductionInfoByDate(string stadate, string endate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<EstimateProductionInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<EstimateProductionInfo>("EstimateProductionInfo", "GetEstimateProductionInfoByDate", stadate, endate);
                return lmSeedlist;
            }
        }


        /// <summary>
        /// 查询领料单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMaterialInfo> GetReceiveMaterialInfoByQP(string rationdate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterialInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMaterialInfo>("EstimateProductionInfo", "GetQDInfo", rationdate);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 查询领料单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMaterialInfo> GetReceiveMaterialInfoByCJG(string rationdate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterialInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMaterialInfo>("EstimateProductionInfo", "GetCJGInfo", rationdate);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 根据批次号查询领料单加工单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetReceiveMachSchemeInfo(string rationdate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetReceiveMachSchemeInfo", rationdate);
                return lmSeedlist;
            }
        }
        /// <summary>
        /// 根据批次号查询领料单加工单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetReceiveMachSchemeInfoByJC(string rationdate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetReceiveMachSchemeInfoByJC", rationdate);
                return lmSeedlist;
            }
        }
        /// <summary>
        /// 根据批次号查询领料单加工单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetReceiveMachSchemeInfoByFJC(string rationdate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetReceiveMachSchemeInfoByFJC", rationdate);
                return lmSeedlist;
            }
        }



        /// <summary>
        /// 根据时间查询领料单加工单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetReceiveMachSchemeInfoByDate(string stadate, string enddate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetReceiveMachSchemeInfoByDate", stadate, enddate);
                return lmSeedlist;
            }
        }


        /// <summary>
        /// 根据时间查询领料单加工单信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMaterialInfo> GetReceiveMaterialInfoByDate(string stadate, string enddate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMaterialInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMaterialInfo>("EstimateProductionInfo", "GetReceiveMaterialInfoByDate", stadate, enddate);
                return lmSeedlist;
            }
        }


        /// <summary>
        /// 查询订单分拣信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<BatchOrderInfo> GetOrderBatchInfo(string stadate, string enddate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<BatchOrderInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<BatchOrderInfo>("EstimateProductionInfo", "GetDCOrderBatchInfo", stadate, enddate);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 查询实际全部波次订单工单
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetSJDDMachSchemeByAll(string stadate, string enddate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetSJDDMachSchemeByAll", stadate, enddate);
                return lmSeedlist;
            }
        }



        /// <summary>
        /// 查询某波次订单工单
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<ReceiveMachSchemeInfo> GetSJDDMachScheme(string stadate, string enddate, string batchNo)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<ReceiveMachSchemeInfo> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ReceiveMachSchemeInfo>("EstimateProductionInfo", "GetSJDDMachScheme", stadate, enddate, batchNo);
                return lmSeedlist;
            }
        }


        /// <summary>
        /// 查询今天有多少个批次
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<OrderBatchGroupBy> GetOrderBatchNumberByAll(string stadate, string enddate)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<OrderBatchGroupBy> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderBatchGroupBy>("EstimateProductionInfo", "GetOrderBatchNumberByAll", stadate, enddate);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 查询今天有多少个批次
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<OrderBatchGroupBy> GetOrderBatchNumber(string stadate, string enddate,string batchNo)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<OrderBatchGroupBy> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderBatchGroupBy>("EstimateProductionInfo", "GetOrderBatchNumber", stadate, enddate, batchNo);
                return lmSeedlist;
            }
        }



        /// <summary>
        /// 查询配送系统订单数据
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<PsOrder> GetPSOrderInfo(string stadate, string enddate, string status, string selType)
        {
            string sqlName = "";
            string estimatedate = "";
            if (selType == "0")
            {
                sqlName = "GetPSOrderInfo";

                enddate = Convert.ToDateTime(enddate).AddDays(1).ToString("yyyy-MM-dd");
            }
            else if (selType == "1")
            {
                sqlName = "GetPSDRDOrderInfo";
                int hour = DateTime.Now.Hour;
                if (hour >= 22 && hour < 24)
                {
                    stadate = DateTime.Now.ToString("yyyy-MM-dd") + " 22:00";
                    enddate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " 11:00";
                }
                else
                {
                    stadate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 22:00";
                    enddate = DateTime.Now.ToString("yyyy-MM-dd") + " 11:00";
                }

                estimatedate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            }
            else if (selType == "2")
            {
                sqlName = "GetPSDRDOrderInfo";
                stadate = DateTime.Now.ToString("yyyy-MM-dd") + " 22:00";
                enddate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " 11:00";
                estimatedate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            }
            else
            {
                sqlName = "GetDRDReceiveMaterial";
            }
            if (selType == "0")
            {
                using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("MsSql")))
                {
                    List<PsOrder> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PsOrder>("EstimateProductionInfo", sqlName, stadate, enddate, status);
                    return lmSeedlist;
                }
            }
            else
            {
                using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("MsSql")))
                {
                    List<PsOrder> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PsOrder>("EstimateProductionInfo", sqlName, stadate, enddate, status, estimatedate);
                    return lmSeedlist;
                }
            }

        }


        /// <summary>
        /// 更新前置仓仓库信息
        /// </summary>
        /// <param name="dCwarehouse"></param>
        /// <returns></returns>
        public int DeleteTempOrder()
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("EstimateProductionInfo", "DeleteTempOrder");
        }



        public bool AddTempOrder(List<PsOrder> orderinfo)
        {
            bool result = false;
            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                if (orderinfo == null || orderinfo.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("EstimateProductionInfo", "AddTempOrder", orderinfo);
                    result = true;
                }
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }


    }
}
